<template>
  <div>
    <el-form>
      <el-form-item label="手机号" :rules="rulesLogin">
        <el-input v-model="formData.mobile" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="formData.passWord" />
      </el-form-item>
      <el-form-item label="再次输入密码">
        <el-input v-model="formData.checkpassword" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    const validCheckPassword = (rule, value, callback) => {
      // rule 规则对象，一般用不到，value 当前输入值，callback 放行的回调
      // 如果合法直接调用callback()
      // 否则，回调时传入一个错误对象
      if (value === this.formData.passWord) {
        callback()
      } else {
        callback(new Error('两次密码不相同'))
      }
    }
    return {
      formData: {
        mobile: '',
        passWord: '',
        checkpassword: ''
      },
      rulesLogin: {
        mobile: [
          { required: true, message: '手机号必填', trigger: 'blur' },
          { pattern: /^1[3-9][0-9]{9}$/, message: '请输入正确手机号', trigger: 'blur' }
        ],
        passWord: [
          { required: true, message: '密码必填', trigger: 'blur' },
          { min: 3, max: 16, message: '密码在 3-16 位之间', trigger: 'blur' }
        ],
        checkpassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { trigger: 'blur', validator: validCheckPassword }
        ]
      }

    }
  }
}
</script>

<style>

</style>
